package abc.red.controller;

import abc.red.entity.Customer;
import abc.red.entity.D;
import abc.red.entity.R;
import abc.red.entity.Warehouse;
import abc.red.service.CustomerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author YiXia
 */
@Slf4j
@RestController
@RequestMapping("/customer")
public class CustomerController {


    @Autowired
    private CustomerService service;
    //默认每页数据量
    private final int pageSize = 10;


    @PostMapping("/getAll")
    public R<IPage> getAll(@RequestBody D d) {


        IPage<Customer> page = new Page<>(d.getNum1(), pageSize);
        //超级管理员可以查询所有
        service.page(page);

        return R.success(page);
    }


    @PostMapping("/add")
    public R<String> add(@RequestBody Customer c) {
        boolean end = service.save(c);
        if (end) {
            log.info("打印 CustomerController-add 方法上传的 Customer 对象数据   {}", c.toString());
            return R.success("新增成功");
        } else {
            return R.error("新增失败");
        }
    }
//
//    @PostMapping("/changeStatus")
//    public R<String> changeStatus(@RequestBody D d) {
//        Warehouse sqlS = service.getById(d.getId());
//        sqlS.setState((sqlS.getState() + 1) % 2);
//        //防手贱代码，保护超级管理员账号
//
//        boolean end = service.updateById(sqlS);
//        log.info("打印 WarehouseController -changeStatus方法上传的 Warehouse 对象数据   {}", sqlS.toString());
//        if (end) {
//            return R.success("修改成功");
//        } else {
//            return R.error("修改失败");
//        }
//    }
//
//
    @PostMapping("/deleteById")
    public R<String> deleteById(@RequestBody Customer c) {

        boolean end = service.removeById(c);
        if (end) {
            log.info("打印 CustomerController -deleteById方法上传的 Customer 对象数据   {}", c.toString());
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }
//
//
    @PostMapping("/edit")
    public R<String> editById(@RequestBody Customer c) {
        boolean end = service.updateById(c);
        if (end) {
            log.info("打印 CustomerController -edit方法上传的 Customer 对象数据   {}", c.toString());
            return R.success("编辑成功");
        } else {
            return R.error("编辑失败");
        }
    }
//
//
    @PostMapping("/getOneById")
    public R<Customer> editById(@RequestBody D d) {
        return R.success(service.getById(d.getNum1()));
    }
//
//
//    @PostMapping("/getAllWarehouseList")
//    public List<Warehouse> getAllWarehouseList() {
//        long id = BaseContext.getCurrentId();
//        LambdaQueryWrapper<Warehouse> lqw = new LambdaQueryWrapper<>();
//        if (id == 1) {
//            lqw.eq(Warehouse::getState,1);
//        } else {
//            lqw.eq(Warehouse::getId, id).eq(Warehouse::getState,1);
//        }
//        return service.list(lqw);
//    }


}
